//
//  PictureLoadingView.swift
//  新浪微博01
//
//  Created by 李国伟 on 15/12/19.
//  Copyright © 2015年 李国伟. All rights reserved.
//

import UIKit
class PictureLoadingView: UIImageView {

    var progress : CGFloat = 0 {
        didSet{
            progressView.progress = progress
        }
    }
    
     init() {
        super.init(frame: CGRectZero)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI(){
        addSubview(progressView)
        progressView.backgroundColor = UIColor.clearColor()
        progressView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(self)
        }
    }
    
    private lazy var progressView: ProgressView = ProgressView()
}

//重绘进度
private class ProgressView : UIView {
    
    //接收到信息进行重绘
    var progress: CGFloat = 0 {
        didSet{
            setNeedsDisplay()
        }
    }
    
    private override func drawRect(rect: CGRect) {
        
        let center = CGPointMake(rect.width * 0.5, rect.height * 0.5)
        let radiu = min(rect
            .width, rect.height) * 0.5
        let start = CGFloat(-M_PI_2)
        let end = start + progress * 2 * CGFloat(M_PI)
        let path = UIBezierPath(arcCenter: center, radius: radiu, startAngle: start, endAngle: end, clockwise: true)
        UIColor(white: 1, alpha: 0.5).setFill()
        path.addLineToPoint(center)
        path.closePath()
        path.fill()
    }
}